﻿using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using SoonCode.FocusTest.Events;
using System.Collections.ObjectModel;
using System.Windows.Input;
using SoonCode.Core;
using SoonCode.FocusTest.Models;
using SoonCode.FocusTest.Utils;
using System.Windows;

namespace SoonCode.FocusTest.ViewModels
{
    internal class AnswerItemEViewModel:BindableBase
    {
        private ObservableCollection<EItemHelper> answerItems;
        private IEventAggregator aggregator;
        private string checkImg = "";
        private int currTime = 0;
        private int lastIndex = -1;
        private string buttonContent = "下一页";
        private int tabIndex = 0;
        private int level;
        private string tipDemoImg = "pack://application:,,,/Images/e_demo.png";

        public string TipDemoImg
        {
            get { return tipDemoImg; }
            set
            {
                tipDemoImg = value;
                RaisePropertyChanged();
            }
        }
        public int TabIndex
        {
            get { return tabIndex; }
            set
            {
                tabIndex=value;
                RaisePropertyChanged();
            }
        }

        public string ButtonContent
        {
            get { return buttonContent; }
            set
            {
                buttonContent = value;
                RaisePropertyChanged();
            }
        }

        public string CheckImg
        {
            get { return checkImg; }
            set
            {
                checkImg=value;
                RaisePropertyChanged();
            }
        }

        public int CurrTime
        {
            get { return currTime; }
            set
            {
                currTime = value;
                RaisePropertyChanged();
            }
        }

        public ObservableCollection<EItemHelper> AnswerItems
        {
            get { return answerItems; }
            set
            {
                answerItems = value;
                RaisePropertyChanged();
            }
        }
        public ICommand NextPageCommand { get; set; }
        public ICommand AnswerCommand { get; set; }
        public AnswerItemEViewModel(IEventAggregator aggregator)
        {   
            answerItems =new ObservableCollection<EItemHelper>();
            this.aggregator = aggregator;
            NextPageCommand = new DelegateCommand(NextPage);
            this.aggregator.GetEvent<TestLoadEvent>().Subscribe(OnLoaded);
            this.aggregator.GetEvent<LevelUpdateEvent>().Subscribe(info =>
            {
                if (info.Type != "itemE") return;
                TabIndex = 1;
                AnswerItems = (ObservableCollection<EItemHelper>)(info.Data["data"]);
                string imgSux = GlobalModel.Instance.MemberInfo.age >= 5 ? "" : "t";
                CheckImg = $"pack://application:,,,/Images/e{imgSux}{info.Data["checkKey"]}.png"; ;
                level = info.Data["level"].ToString().ToInt();
                lastIndex = -1;
                if ( level>= 4)
                {
                    ButtonContent = "提交";
                }
            });
            this.aggregator.GetEvent<QuestionChangeEvent>().Subscribe(info =>
            {
                if (info.Type != "itemE") return;
                CurrTime = (int)info.Data["currTime"];
            });
            this.AnswerCommand = new DelegateCommand<int?>(StudentAnswer);
        }

        private void OnLoaded(int p)
        {
            if (p != 4) return;
            TabIndex = 0;
            lastIndex = -1;
            CurrTime = 0;
            AnswerItems.Clear();
            ButtonContent = "下一页";
            int age=GlobalModel.Instance.MemberInfo.age;
            TipDemoImg = age >= 5 ? "pack://application:,,,/Images/e_demo.png" : "pack://application:,,,/Images/e_demo2.png";
        }

        public void NextPage()
        {
            EventInfo info = new EventInfo();
            info.Type = "itemE_Next";
            this.aggregator.GetEvent<StudentAnswerEvent>().Publish(info);
            if (level >= 4)
            {
                buttonContent = "提交";
            }
        }

        private void StudentAnswer(int? p)
        {
            if (p == null) return;
            if (p <= lastIndex) return;
            SoundPlayer.PlayButton();
            lastIndex = p.Value;
            answerItems[lastIndex].selectColor = "Chocolate";
            EventInfo answerInfo = new EventInfo();
            answerInfo.Type = "itemE";
            answerInfo.Data["info"] = AnswerItems[lastIndex];
            this.aggregator.GetEvent<StudentAnswerEvent>().Publish(answerInfo);
        }
    }
}
